/*
 * Copyright 2011 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;

import de.wenzelmf.booleanFunction.lpb.util.LPB;

/**
 * This class can be used to read a file that contains in each line an LPB.
 * 
 * The input file must contain a LPB in each line (LPB syntax as in {@link BooleanFunctionRecognitionParser}).
 * 
 * Lines starting with # and that do not contain a valid LPB will be ignored.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 */
public class LPBFileReader
{
	/**
	 * The reader that is used to read the lines of the file.
	 */
	private BufferedReader reader;
	/**
	 * The parser that is used to parse the LPBs.
	 */
	private BFParser parser;
	
	/**
	 * Constructs a new LPBFileReader.
	 * 
	 * @param file The file to read from.
	 * 
	 * @throws IOException If an error occurred opening the file.
	 */
	public LPBFileReader(File file) throws IOException
	{
		reader = new BufferedReader(new FileReader(file));
		parser = new BFParser(new StringReader(""));
	}
	
	/**
	 * Reinitialize this reader with a new file.
	 * 
	 * @param file The file to read from.
	 * 
	 * @throws IOException If an error occurred opening the file.
	 */
	public void reInit(File file) throws IOException
	{
		reader = new BufferedReader(new FileReader(file));
	}
	
	/**
	 * Returns the next LPB in the file and {@code null} if there are no more LPBs.
	 * 
	 * A line starting with # is ignored. Line that do not contain an LPB are omitted.
	 * 
	 * @return The next LPB in the file and {@code null} if there are no more LPBs.
	 * 
	 * @throws IOException If an error occurred while reading the lines of the file.
	 */
	public LPB next() throws IOException
	{
		if(reader == null)
			return null;
		// Try to find next LPB in file
		String line;
		while((line = reader.readLine()) != null)
		{
			if(line.equals("") || line.equals("\n") || line.trim().startsWith("#"))
				continue;
			else
			{
				// try to parse it as LPB
				StringReader sReader = new StringReader(line);
				parser.ReInit(sReader);
				try
				{
					return parser.LPB();
				}
				catch(ParseException e)
				{
					continue;
				}
			}
		}
		// we found no LPB, so return null
		// but first close the BufferedReader and set it to null
		reader.close();
		reader = null;
		return null;
	}
	
}
